package com.atguigu.educenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.service.UcenterMemberService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author 25169
 * @since 2023-01-24
 */
@RestController
@RequestMapping("/educenter/ucentermember")
public class UcenterMemberController {

    private static final Logger logger = LoggerFactory.getLogger(UcenterMemberController.class);
    @Autowired
    private UcenterMemberService memberService;

    /**
     * 会员登录
     * @param member
     * @return
     */
    @PostMapping("login")
    public R login(@RequestBody UcenterMember member){
        String token=memberService.login(member);
        return R.ok().data("token",token);
    }

    /**
     * 注册
     * @param registerVo
     * @return
     */
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo){
        memberService.register(registerVo);
        return R.ok();
    }

    /**
     * 根据token获取登录信息
     * @param request
     * @return
     */
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        logger.info("memberId为：{}",memberId);
        UcenterMember ucenterMember = memberService.getById(memberId);
        logger.info("ucenterMember为:{}",ucenterMember);
        return R.ok().data("ucenterMember",ucenterMember);
    }

    /**
     * 根据id查询用户信息的接口
     * @param id
     * @return
     */
    @GetMapping("getInfo")
    public com.atguigu.commonutils.UcenterMember getInfo(String id){
        UcenterMember byId = memberService.getById(id);
        com.atguigu.commonutils.UcenterMember ucenterMember=new com.atguigu.commonutils.UcenterMember();
        BeanUtils.copyProperties(byId,ucenterMember);
        return ucenterMember;
    }

    @GetMapping("getInfos/{id}")
    public Map<String,String> getInfos(@PathVariable String id){
        UcenterMember byId = memberService.getById(id);

        Map<String,String> map=new HashMap<>();

        map.put("Nickname",byId.getNickname());
        map.put("Id",byId.getId());
        map.put("Mobile",byId.getMobile());
        return map;
    }

    /**
     * 查询某一天有多少注册人数
     * @param day
     * @return
     */
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
        logger.info("传过来的日期值为：{}",day);
        Integer count=memberService.countRegisterDay(day);
        return R.ok().data("count",count);
    }

}

